package com.wanmei.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil {
    static Message message ;
    static Properties properties;
    static Transport transport;
    /**
     * 初始化邮箱信息
     */
    public MailUtil(){
        properties =new Properties();
        //调试模式
        properties.put("mail.debug","true");
        //发送协议
        properties.put("mail.transport.protocol","smtp");
        //主机地址
        properties.put("mail.smtp.host", "smtp.qq.com");
        //端口号
        properties.put("mail.smtp.port", 465);
        //是否使用Ssl安全链接.
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");

        Session session =Session.getInstance(properties);
        try {
            message = new MimeMessage(session);
            message.setFrom(new InternetAddress("466769626@qq.com"));//发件人
            message.setSubject("完美商城发来的一封邮件");//邮件标题
             // 由Controller发来内容和发件人姓名。
//            message.setText("aaaabbbb");
//            message.setRecipients(Message.RecipientType.TO,new InternetAddress[]{new InternetAddress("835838004@qq.com")});
//
            transport =session.getTransport();
            transport.connect("466769626@qq.com","ahmvpjqhziznbiag");
//            transport.sendMessage(message,message.getAllRecipients());
        }catch (MessagingException e){
            e.printStackTrace();
        }
    }
    public void sendMail(String address,String text) throws MessagingException {
        message.setText("尊敬的用户，本次您的验证码为"+text+",谢谢您！");
        transport.sendMessage(message,new Address[]{new InternetAddress(address)});

        transport.close();
    }

}
